// import { ElMessage } from 'element-plus'
import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'

const router = createRouter({
    history: createWebHistory(
        import.meta.env.BASE_URL),
    routes: [{
            path: '/',
            redirect: '/home',

        },
        {
            path: '/home',
            name: 'home',
            component: () =>
                import ('@/views/HomeView.vue'),
            children: [{
                    path: 'manage',
                    label: '账户管理',
                    component: () =>
                        import ('@/views/manage/manageIndex.vue'),
                    meta: {
                        name: '账户管理'
                    },
                    children: [{
                        path: 'managelist',
                        name: 'managelist',
                        label: '管理员列表',
                        component: () =>
                            import ('@/views/manage/manageListView.vue'),
                        meta: {
                            name: '管理员列表'
                        },
                    }]
                },
                {
                    path: 'swipermanage',
                    name: "swipermanage",
                    label: '轮播图管理',
                    meta: {
                        name: "轮播图管理"
                    },
                    component: () =>
                        import ('@/views/swipermanage/manageIndex.vue'),
                    children: [{
                            path: 'swiperlist',
                            name: "swiperlist",
                            label: '轮播图列表',
                            meta: {
                                name: "轮播图列表"
                            },
                            component: () =>
                                import ('@/views/swipermanage/swiperListView.vue')
                        },
                        {
                            path: 'addswiper',
                            name: "addswiper",
                            label: '添加轮播图',
                            meta: {
                                name: "添加轮播图"
                            },
                            component: () =>
                                import ('@/views/swipermanage/addswiperView.vue')
                        }
                    ]
                },
                {
                    path: 'productmanage',
                    name: "productmanage",
                    label: '产品管理',
                    meta: {
                        name: "产品管理"
                    },
                    component: () =>
                        import ('@/views/productmanage/manageIndexView.vue'),
                    children: [{
                            path: 'productlist',
                            name: "productlist",
                            label: '产品列表',
                            meta: {
                                name: "产品列表"
                            },
                            component: () =>
                                import ('@/views/productmanage/productlistView.vue')
                        },
                        {
                            path: 'secondkilllist',
                            name: "secondkilllist",
                            label: '秒杀列表',
                            meta: {
                                name: "秒杀列表"
                            },
                            component: () =>
                                import ('@/views/productmanage/secondkilllist.vue')
                        },
                        {
                            path: 'recomendlist',
                            name: "recomendlist",
                            label: '推荐列表',
                            meta: {
                                name: "推荐列表"
                            },
                            component: () =>
                                import ('@/views/productmanage/recomendListView.vue')
                        },
                        {
                            path: 'filterlist',
                            name: "filterlist",
                            label: '筛选列表',
                            meta: {
                                name: "筛选列表"
                            },
                            component: () =>
                                import ('@/views/productmanage/filterlistView.vue')
                        }
                    ]
                },
                {
                    path: 'echartsmanage',
                    name: "echartsmanage",
                    label: '数据可视化',
                    meta: {
                        name: "数据可视化"
                    },
                    component: () =>
                        import ('@/views/echarts/echartIndex.vue'),
                    children: [{
                            path: 'echartsshow',
                            name: "echartsshow",
                            label: 'Echarts数据展示',
                            meta: {
                                name: "Echarts数据展示"
                            },
                            component: () =>
                                import ('@/views/echarts/echartShowdata.vue')
                        },
                        {
                            path: 'echartsother',
                            name: "echartsother",
                            label: 'Echarts其他数据',
                            meta: {
                                name: "Echarts其他数据"
                            },
                            component: () =>
                                import ('@/views/echarts/echartsOtherView.vue')
                        }
                    ]
                },
                {
                    path: 'editor',
                    name: "editor",
                    label: '编辑器',
                    component: () =>
                        import ('@/views/editor/editorIndex.vue'),
                    meta: {
                        name: "编辑器"
                    },
                    children: [{
                        path: 'editorsubmit',
                        name: "editorsubmit",
                        label: '富文本编辑器',
                        meta: {
                            name: "富文本编辑器"
                        },
                        component: () =>
                            import ('@/views/editor/editorSubmitView.vue')
                    }]
                },
                {
                    path: 'map',
                    name: "map",
                    label: '地图',
                    component: () =>
                        import ('@/views/map/mapIndex.vue'),
                    meta: {
                        name: "地图"
                    },
                    children: [{
                        path: 'mapbaidu',
                        name: "mapbaidu",
                        label: '我的地图',
                        meta: {
                            name: "我的地图"
                        },
                        component: () =>
                            import ('@/views/map/mapBaiduView.vue')
                    }]
                }

            ]

        }, {
            path: '/login',
            name: 'login',
            component: () =>
                import ('@/views/LoginView.vue')
        },
    ]
})


// 设置全局前置守卫
router.beforeEach((to, from) => {
    if (to.path != '/login') {
        if (!localStorage.getItem('token')) {
            // 表示本地没有token,
            // 获取的值为null
            ElMessage({
                message: '用户未登录,请先登录!!!',
                type: 'warning',
                duration: 2000,
            })
            return { path: '/login' }
        }
    } else {
        // 进入登录页
        if (localStorage.getItem('token')) {
            // 判断有没有token,有的话,友好提示
            ElMessage({
                message: '登录状态有效,不需要重复登录!!!',
                type: 'warning',
                duration: 2000,
            })
            return {
                path: from.path
            }
        }

    }

})

export default router